﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;

namespace WindowsService.WCF
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码和配置文件中的类名“WCFService”。
    
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class WCFService : IWCFService
    {
        static IList<Person> Persons = new List<Person> 
        {
            new Person{ id="001".ToString(),name="zhangsan", sex=true, age=20},
            new Person{ id="002".ToString(),name="lisi", sex=true, age=20},
            new Person{ id="003",name="wanger", sex=true, age=20},
            new Person{ id="004".ToString(),name="zhaoliu", sex=true, age=20}
        };

        public IList<Person> All()
        {
            return Persons;
        }

        public Person GetPerson(string id)
        {
            return Persons.Where(m => m.id == id).SingleOrDefault();
        }

        public OperationFeedback UpdatePerson(string id, string name, bool sex, int age)
        {
            Person person = Persons.Where(m => m.id == id).SingleOrDefault();
            if (person != null)
            {
                person.name = name;
                person.sex = sex;
                person.age = age;
            }
            return new OperationFeedback { result = true, data = person };
        }

        public OperationFeedback AddPerson(Person person)
        {
            Persons.Add(person);

            return new OperationFeedback { result = true, data = person };
        }

        public bool DeletePerson(string id)
        {
            var person= Persons.Where(m => m.id == id).SingleOrDefault();
            if (person != null)
            {
                Persons.Remove(person);
            }
            return true;
        }

        public Int32 IndexOf(Person person)
        {
            return Persons.IndexOf(person);
        }
    }
}
